package com.sg.plugin.obff.exception;


import com.wicket.okrobff.common.enums.ResponseStateEnum;
import com.wicket.okrobff.common.exception.BizException;
import com.sg.common.ro.OkrResult;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;


@Slf4j
@ControllerAdvice
public class ExceptionControllerAdvice {

    private static String ROW_NEXT = "\n";

    @Autowired
    private MessageSource messageSource;


    @ResponseBody
    @ExceptionHandler(value = {BizException.class})
    public OkrResult<String> bizException(BizException ex) {

        String message = "";
        if (StringUtils.isBlank(message)) {
            message = ex.getMessage();
        }
        log.warn(message + ROW_NEXT + ExceptionUtils.getStackTrace(ex));
        if (message != null && message.contains("Duplicate entry")) {
            message = "对不起，不能重复新增";
        }

        if (message != null && message.contains("Deadlock")) {
            message = "数据处理中，请稍后再试～";
        }
        if (ex.getErrorCode().equals("403")) {
            return OkrResult.fail("403", message);
        } else {
            return OkrResult.fail("888", message);
        }

    }


    @ResponseBody
    @ExceptionHandler(value = {Exception.class})
    public OkrResult<String> handleException(Exception ex) {
        log.warn(ExceptionUtils.getStackTrace(ex));
        String message = "";
        if (ex != null && ex.getMessage() != null && ex.getMessage().contains("Duplicate entry")) {
            message = "对不起，不能重复新增";
        }
        if (ex != null && ex.getMessage() != null && ex.getMessage().contains("Deadlock")) {
            message = "数据处理中，请稍后再试～";
        }
        return OkrResult.fail("888", message);
    }

    @ResponseBody
    @ExceptionHandler(MethodArgumentNotValidException.class)
    public OkrResult<String> validationBodyException(MethodArgumentNotValidException exception) {
        BindingResult result = exception.getBindingResult();
        String message = "";
        if (result.hasErrors()) {
            List<ObjectError> errors = result.getAllErrors();
            errors.forEach(p -> {
                FieldError fieldError = (FieldError) p;
                log.debug(
                        "Data check failure : object{" + fieldError.getObjectName() + "},field{" + fieldError
                                .getField() +
                                "},errorMessage{" + fieldError.getDefaultMessage() + "}");

            });
            FieldError fieldError = (FieldError) errors.get(0);
            message = fieldError.getDefaultMessage();
        }
        log.warn(ExceptionUtils.getStackTrace(exception) + message);
        return OkrResult.fail(ResponseStateEnum.FAILURE.getCode(), message);
    }
}
